<?php


namespace app\chat\controller;
use app\chat\controller\Base;
use think\Request;
use app\chat\model\ChatUser as ChatUserModel;

class User extends Base
{
    //登录
    public function login(Request $request){
        if (session('user')){
            return $this->redirect(url('chat/index/chat'));
        }
        if ($request->isAjax()){
            $return = [
                'state' => 'error',
                'msg'  => '',
                'data' => ''
            ];
            $data = $request->post();
            if (empty($data)){
                $return['msg'] = '参数错误';
                return json($return);
            }
            if (empty($data['name'])){
                $return['msg'] = '请输入用户名';
                return json($return);
            }
            if (empty($data['pwd'])){
                $return['msg'] = '请输入密码';
                return json($return);
            }

            $user = ChatUserModel::where(['username'=>$data['name'],'pw'=>md5($data['pwd'])])->find();
            if (empty($user)){
                $return['msg'] = '密码错误';
                return json($return);
            }
            if ($user['state'] != 1){
                $return['msg'] = '账号已停用，请联系管理员';
                return json($return);
            }
            session('user',$user);
            $return['state'] = 'success';
            $return['msg']   = '登录成功';
            return json($return);
        }
       return $this->fetch();
    }

    //注册
    public function register(Request $request){
        if ($request->isAjax()){
            $return = [
               'state' => 'error',
               'msg'  => '',
               'data' => ''
             ];

            $data = $request->post();
            if (empty($data)){
                $return['msg'] = '参数错误';
                return json($return);
            }
            if (empty($data['name'])){
                $return['msg'] = '请输入用户名';
                return json($return);
            }
            if (mb_strlen($data['name'],'utf8') > 20){
                $return['msg'] = '用户名超出限制';
                return json($return);
            }
            if (empty($data['pwd'])){
                $return['msg'] = '请输入用户名';
                return json($return);
            }
            if ($data['pwd'] !== $data['pwd1']){
                $return['msg'] = '两次密码不一样';
                return json($return);
            }

            $user = ChatUserModel::where(['username'=>$data['name']])->select();
            if(!empty($user)){
                $return['msg'] = '用户名已被占用，请重新输入！';
                return json($return);
            }
            $user = [
                'name'       => '',
                'username'   => $data['name'],
                'pw'         => md5($data['pwd']),
                'add_time'   => time(),
                'state'      => 1,
                'login_time' => time(),
                'login_ip'   => $request->ip(),
                'role'       => 0,
                'group_id'   => 5
            ];

            $res = ChatUserModel::create($user);
            if (empty($res)){
                $return['msg'] = "注册失败";
                return json($return);
            }
            session('user',$res);
            $return['state'] = 'success';
            $return['msg']   = '注册成功';
            return json($return);
            // 普通用户登录
        }
        return $this->fetch();
    }
}